package com.ruoyi.bussiness.service.impl;

import java.util.List;

import com.ruoyi.bussiness.domain.SysBusinessCheckList;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bussiness.mapper.SysBusinessPolicyMapper;
import com.ruoyi.bussiness.domain.SysBusinessPolicy;
import com.ruoyi.bussiness.service.ISysBusinessPolicyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 业务_政策Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-18
 */
@Service
public class SysBusinessPolicyServiceImpl implements ISysBusinessPolicyService
{
    @Autowired
    private SysBusinessPolicyMapper sysBusinessPolicyMapper;

    /**
     * 查询业务_政策
     *
     * @param policyId 业务_政策主键
     * @return 业务_政策
     */
    @Override
    public SysBusinessPolicy selectSysBusinessPolicyByPolicyId(Long policyId)
    {
        return sysBusinessPolicyMapper.selectSysBusinessPolicyByPolicyId(policyId);
    }

    /**
     * 查询业务_政策列表
     *
     * @param sysBusinessPolicy 业务_政策
     * @return 业务_政策
     */
    @Override
    public List<SysBusinessPolicy> selectSysBusinessPolicyList(SysBusinessPolicy sysBusinessPolicy)
    {
        return sysBusinessPolicyMapper.selectSysBusinessPolicyList(sysBusinessPolicy);
    }

    /**
     * 新增业务_政策
     *
     * @param sysBusinessPolicy 业务_政策
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysBusinessPolicy(SysBusinessPolicy sysBusinessPolicy)
    {
        sysBusinessPolicy.setCreateTime(DateUtils.getNowDate());
        sysBusinessPolicy.setIsBottom("0");
        sysBusinessPolicy.setIsTop("0");
        sysBusinessPolicy.setCreateBy(SecurityUtils.getUsername());
        int i = sysBusinessPolicyMapper.insertSysBusinessPolicy(sysBusinessPolicy);

        sysBusinessPolicyMapper.updateOrderNum(sysBusinessPolicy.getPolicyId());
        return i;
    }

    /**
     * 修改业务_政策
     *
     * @param sysBusinessPolicy 业务_政策
     * @return 结果
     */
    @Override
    public int updateSysBusinessPolicy(SysBusinessPolicy sysBusinessPolicy)
    {
        sysBusinessPolicy.setUpdateTime(DateUtils.getNowDate());
        sysBusinessPolicy.setUpdateBy(SecurityUtils.getUsername());
        return sysBusinessPolicyMapper.updateSysBusinessPolicy(sysBusinessPolicy);
    }

    /**
     * 批量删除业务_政策
     *
     * @param policyIds 需要删除的业务_政策主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessPolicyByPolicyIds(Long[] policyIds)
    {
        return sysBusinessPolicyMapper.deleteSysBusinessPolicyByPolicyIds(policyIds);
    }

    /**
     * 删除业务_政策信息
     *
     * @param policyId 业务_政策主键
     * @return 结果
     */
    @Override
    public int deleteSysBusinessPolicyByPolicyId(Long policyId)
    {
        return sysBusinessPolicyMapper.deleteSysBusinessPolicyByPolicyId(policyId);
    }

    @Override
    public AjaxResult moveUpOrDown(SysBusinessPolicy entity) {
        SysBusinessPolicy moveEntity = sysBusinessPolicyMapper.selectSysBusinessPolicyByPolicyId(entity.getPolicyId());
        if(moveEntity == null){
            return AjaxResult.error("政策数据不存在，不能上移下移");
        }
        SysBusinessPolicy moveChangeEntity = null;
        if("up".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessPolicyMapper.getLastList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("首行数据不能上移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessPolicyMapper.updateSysBusinessPolicy(moveEntity);
            sysBusinessPolicyMapper.updateSysBusinessPolicy(moveChangeEntity);
        }else if("down".equalsIgnoreCase(entity.getMoveType())){
            moveChangeEntity = sysBusinessPolicyMapper.getNextList(moveEntity.getOrderNum());
            if(moveChangeEntity == null){
                return AjaxResult.error("末行数据不能下移");
            }
            Integer orderNum = moveEntity.getOrderNum();
            moveEntity.setOrderNum(moveChangeEntity.getOrderNum());
            moveChangeEntity.setOrderNum(orderNum);
            sysBusinessPolicyMapper.updateSysBusinessPolicy(moveEntity);
            sysBusinessPolicyMapper.updateSysBusinessPolicy(moveChangeEntity);
        }else{
            return AjaxResult.error("无效的移动方式");
        }
        return AjaxResult.success("移动成功",1);
    }

    @Override
    public AjaxResult pinToTopOrEnd(SysBusinessPolicy entity) {
        SysBusinessPolicy moveEntity = sysBusinessPolicyMapper.selectSysBusinessPolicyByPolicyId(entity.getPolicyId());
        if(moveEntity == null){
            return AjaxResult.error("政策不存在，不能上移下移");
        }
        if("top".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsTop())){
                moveEntity.setIsTop("0");
                moveEntity.setIsBottom("0");
            }else{
                moveEntity.setIsTop("1");
                moveEntity.setIsBottom("0");
            }
            sysBusinessPolicyMapper.updateSysBusinessPolicy(moveEntity);
        }else if("end".equalsIgnoreCase(entity.getPinType())){
            if("1".equalsIgnoreCase(moveEntity.getIsBottom())){
                moveEntity.setIsBottom("0");
                moveEntity.setIsTop("0");
            }else{
                moveEntity.setIsBottom("1");
                moveEntity.setIsTop("0");
            }
            sysBusinessPolicyMapper.updateSysBusinessPolicy(moveEntity);
        }else{
            return AjaxResult.error("无效的操作方式");
        }
        return AjaxResult.success("置顶置底成功",1);
    }
}
